﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonSlider : MonoBehaviour
{
    public Slider slider;
    public Text text;
    public System.Action<float> onValueChanged;
    public float minValue
    {
        get { return slider.minValue; }
        set { slider.minValue = value; }
    }
    public float maxValue
    {
        get { return slider.maxValue; }
        set { slider.maxValue = value; }
    }
    public float value
    {
        get { return slider.value; }
        set { slider.value = value; }
    }
    public bool editable
    {
        set
        {
            var btns = GetComponentsInChildren<Button>(true);
            foreach (var btn in btns)
            {
                btn.enabled = value;
            }
            var sliders = GetComponentsInChildren<Slider>(true);
            foreach (var sld in sliders)
            {
                sld.enabled = value;
            }
        }
    }
    public void Select0()
    {
        slider.value = 0;
    }
    public void SelectAll()
    {
        slider.value = slider.maxValue;
    }
    public void Plus()
    {
        slider.value += 1;
    }
    public void Minus()
    {
        slider.value -= 1;
    }
    void Start()
    {
        slider = GetComponentInChildren<Slider>();
        slider.onValueChanged.AddListener(OnValueChanged);
        if (onValueChanged != null) onValueChanged(slider.value);
    }
    public void Init(Vector3 mom, System.Action<float> ovc)
    {
        slider.minValue = mom.x;
        slider.maxValue = mom.z;
        slider.value = mom.y;
        onValueChanged = ovc;
    }
    void OnValueChanged(float value)
    {
        text.text = value.ToString();
        if (onValueChanged != null) onValueChanged(slider.value);
    }
}
